﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="../../Script/boot.js" type="text/javascript"></script>
    <style type="text/css">
        html, body
        {
            font-size: 12px;
            padding: 0;
            margin: 0;
            border: 0;
            height: 100%;
            overflow: hidden;
        }
    </style>
</head>
<body>
    <form id="form1" method="post">
    <input name="id" class="mini-hidden" />
    <fieldset style="border: solid 1px #aaa; padding: 3px;">
        <legend>基本信息</legend>
        <div style="padding: 5px;">
            <table>
                <tr>
                    <td style="width: 70px;">
                        用&nbsp;户&nbsp;名：
                    </td>
                    <td style="width: 150px;">
                        <input name="userName" class="mini-textbox" required="true" onvalidation="onUserNameValidation" />
                    </td>
                </tr>
                <tr>
                    <td>
                        用户密码：
                    </td>
                    <td>
                        <input name="password" class="mini-password" required="true" />
                    </td>
                </tr>
                <tr>
                    <td>
                        密码确认：
                    </td>
                    <td>
                        <input id="passwordAgin" class="mini-password" required="true" onvalidation="onPwdCheck" />
                    </td>
                </tr>
                <tr>
                    <td>
                        生产厂商：
                    </td>
                    <td>
                        <input name="factoryNo" class="mini-combobox" valuefield="factoryNo" textfield="factoryName"
                            url="../Commodity/AjaxService.aspx?method=GetFactorys" required="true" />
                    </td>
                </tr>
                <tr>
                    <td>
                        备&nbsp;&nbsp;&nbsp;&nbsp;注：
                    </td>
                    <td>
                        <textarea class="mini-textarea" name="remark" emptyText="请输入备注"></textarea>
                    </td>
                </tr>
                <tr>
                    <td>
                        用户权限：
                    </td>
                    <td>
                        <input name="roleId" class="mini-combobox" valuefield="id" textfield="roleName"
                            url="AjaxService.aspx?method=GetRoles" required="true" />
                    </td>
                </tr>
                <tr>
                    <td>
                        权限列表：
                    </td>
                    <td>
                        <div name="limits" id="combobox2" class="mini-combobox" style="width: 150px;" popupwidth="200"
                            textfield="menuName" valuefield="id" url="AjaxService.aspx?method=GetLimits" value="" multiselect="true">
                            <div property="columns">
                                <div header="权限名称" field="menuName">
                                </div>
                                
                            </div>
                        </div>
                    </td>
                </tr>
            </table>
        </div>
    </fieldset>
    <div style="text-align: center; padding: 10px;">
        <a class="mini-button" onclick="onOk" style="width: 60px; margin-right: 20px;">确定</a>
        <a class="mini-button" onclick="onCancel" style="width: 60px;">取消</a>
    </div>
    </form>
    <script type="text/javascript">
        mini.parse();

        var form = new mini.Form("form1");

        function SaveData() {
            var o = form.getData();

            form.validate();
            if (form.isValid() == false) return;

            var json = mini.encode([o]);
            $.ajax({
                url: "AjaxService.aspx?method=SaveUser",
                data: { data: json },
                cache: false,
                success: function (text) {
                    CloseWindow("save");
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert(jqXHR.responseText);
                    CloseWindow();
                }
            });
        }

        ////////////////////
        //标准方法接口定义
        function SetData(data) {
            if (data.action == "edit") {
                //跨页面传递的数据对象，克隆后才可以安全使用
                data = mini.clone(data);

                $.ajax({
                    url: "AjaxService.aspx?method=GetUser&id=" + data.id,
                    cache: false,
                    success: function (text) {
                        var o = mini.decode(text);
                        form.setData(o);
                        form.setChanged(false);
                        //加载编辑页面的时候需要对页面元素进行操作
                        var pwdAgain = mini.get("passwordAgin");
                        pwdAgain.setValue(o.password);
                        var userName = mini.getbyName("userName");
                        userName.enabled = false;
                    }
                });
            }
        }

        function GetData() {
            var o = form.getData();
            return o;
        }
        function CloseWindow(action) {
            if (action == "close" && form.isChanged()) {
                if (confirm("数据被修改了，是否先保存？")) {
                    return false;
                }
            }
            if (window.CloseOwnerWindow) return window.CloseOwnerWindow(action);
            else window.close();
        }
        function onOk(e) {
            SaveData();
        }
        function onCancel(e) {
            CloseWindow("cancel");
        }

        ///////////////////////////////////////////////////////////
        function onUserNameValidation(e) {
            if (e.isValid) {
                var userName = mini.getbyName("userName");
                if (userName.enabled == false) {
                    e.isValid = true;
                    return;
                }
                if (isEnglishAndNumber(e.value) == false) {
                    e.errorText = "必须输入英文+数字";
                    e.isValid = false;
                    return;
                }
                var result = isUserNameExist(e.value);
                if (result == false) {
                    e.errorText = "用户名已经存在";
                    e.isValid = false;
                }
            }
        }

        function onPwdCheck(e) {
            if (e.isValid) {
                var pwd = mini.getbyName("password");
                if(pwd.value != e.value){
                    e.errorText = "两次密码输入不一致";
                    e.isValid = false;
                }
            }
        }

        ////////////////////////////////////////////////////
        /* 是否英文+数字 */
        function isEnglishAndNumber(v) {
            var re = new RegExp("^[0-9a-zA-Z\_]+$");
            if (re.test(v)) return true;
            return false;
        }

        /* 用户名是否已经存在 */
        function isUserNameExist(value) {
            var result = true;
            $.ajax({
                url: "AjaxService.aspx?method=UserNameValidation&userName=" + value,
                cache: false,
                async: false,
                success: function (text) {
                    if (text == "0") {
                        result = false;
                    } else {
                        result = true;
                    }
                }
            });
            if (result) {
                return true;
            } else {
                return false;
            }
        }

    </script>
</body>
</html>
